<?php
apf_require_class('APF_Controller');
apf_require_class('Solr_Bll_AppBiz');
apf_require_class('Solr_Bll_IdxRequestBiz');
apf_require_class('Solr_Bll_ReportBiz');
apf_require_class('Solr_GlobalFunc');
class Report_CreateDataController extends APF_Controller {

    public static function build_uri() {
        $base_domain = APF::get_instance()->get_config("base_domain");
        $schema = APF::get_instance()->get_request()->is_secure() ? "https" : "http";
        $url = "{$schema}://{$base_domain}/report/createdata/";
        return $url;
    }

    public function handle_request() {
        $request = APF::get_instance()->get_request();
        $params = $request->get_parameters();
        if($params['datei']){
            $date=date('Y-m-d',strtotime($params['datei']));
        }else{
            $date=date('Y-m-d');
        }

        set_time_limit(0);
        $apps_list=Solr_Bll_AppBiz::get_instance()->get_apps_list();
        $idx_request_biz=Solr_Bll_IdxRequestBiz::get_instance();
        foreach($apps_list as $v){
            $stat=$idx_request_biz->get_request_webapp_stat($v['hostname'],$v['port'],$v['webapp'],$date);

            $attr['hostname_id']=$v['host_id'];
            $attr['webapp_id']=$v['webapp_id'];
            $attr['application_id']=$v['id'];
            $attr['datei']=$date;
            $attr['count_min']=$stat['count_min'];
            $attr['slow_min']=$stat['slow_min'];
            $attr['max_count_min']=$stat['max_count_min'];

            $url=$v['hostname'].".i.ajkdns.com";
            $doc_number_url = Solr_GlobalFunc::create_solr_doc_number_url($url,$v['port'],$v['webapp']);
            $solr_res = Solr_GlobalFunc::get_solr_data($doc_number_url);
			$solr_res_result=(array)$solr_res->result;
            $attr['doc_number']=$solr_res_result['@attributes']['numFound'];
            $attr['doc_number_time']=time();
            Solr_Bll_ReportBiz::get_instance()->logic_update_report($attr);
        }
        echo 1;
        return false;
    }

}